package cn.antblog.poi.excel;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YangJingyi
 * @date 2021-12-07 17:07
 */
public class ApachePoiLineChart4 {
    /**
     * 折线图
     */
    public static void test1() throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("linechart");
        final int NUM_OF_ROWS = 3;
        final int NUM_OF_COLUMNS = 10;

        Row row;
        Cell cell;
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
            row = sheet.createRow((short) rowIndex);
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                cell = row.createCell((short) colIndex);
                cell.setCellValue(colIndex * (rowIndex + 1));
            }
        }

        //创建一个画布
        Drawing drawing = sheet.createDrawingPatriarch();
        // 设置位置 起始横坐标，起始纵坐标，终点横，终点纵
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
        //创建chart
        Chart chart = drawing.createChart(anchor);
        // 创建图形注释的位置
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.TOP_RIGHT);
        // 创建绘图的类型 LineCahrtData 折线图
        LineChartData data = chart.getChartDataFactory().createLineChartData();
        // 设置横坐标
        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        // 设置纵坐标
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
        data.addSeries(xs, ys1);
        data.addSeries(xs, ys2);
        chart.plot(data, bottomAxis, leftAxis);
        // 从Excel中获取数据.CellRangeAddress-->params: 起始行号，终止行号， 起始列号，终止列号
        // 数据类别（x轴）
//        ChartDataSource xAxis = DataSources.fromStringCellRange(sheet,
//                new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
//        for (int i = 2, len = params.size(); i < len; i++) {
//            // 数据源
//            ChartDataSource dataAxis = DataSources.fromNumericCellRange(sheet,
//                    new CellRangeAddress(params.get(i)[0], params.get(i)[1], params.get(i)[2], params.get(i)[3]));
//            LineChartSeries chartSeries = data.addSeries(xAxis, dataAxis);
//            // 给每条折线创建名字
//            chartSeries.setTitle(sheet.getRow(params.get(i)[4]).getCell(params.get(i)[5]).getStringCellValue());
//        }
        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("C:\\Users\\YangJingyi\\Desktop\\test2.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }
    public static void main(String[] args) throws IOException {
        List<int []> list = new ArrayList<>();
        int []  ints = new int[]{1,2,3,4,5};
        list.add(ints);

        test1();
    }
}
